package com.mentorgen.tools.util.profile.bundle;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

public class BundleServiceSerializer {

	private static final String AVG_DATA_IN  = "avg-data-in";
	private static final String AVG_DATA_OUT = "avg-data-out";
	private static final String AVG_DATA_TOTAL = "avg-data-total";


	public static String getJSON(BundleContext context){

//		StringBuffer jsonBndInfo = new StringBuffer();
//
//		jsonBndInfo.append("{\"bnds\": {");
//		for (Bundle bundle : context.getBundles()){
//
//			if ( bundle.getRegisteredServices() == null)
//				continue;
//
//			jsonBndInfo.append("\"bnd\": {");
//				jsonBndInfo.append("\"sn\":");
//				jsonBndInfo.append("\"" + Util.getBundleName(bundle) + "\",");
//				jsonBndInfo.append("\"srvcs\":[");
//
//				StringBuffer serviceBuffer = new StringBuffer();
//				for (ServiceReference serviceRef : bundle.getRegisteredServices()){
//					
//					// doesn't consider proxy services as they are not the actual srevices
//					// and don't have to be taken into consideration
//					if (serviceRef.getProperty("proxied") != null)
//						continue;
//
//					serviceBuffer.append("{");
//
//					StringBuffer propertyBuffer = new StringBuffer();
//					for (String key : serviceRef.getPropertyKeys())
//						propertyBuffer.append("\"" + key + "\": \"" + Util.getValueString(serviceRef.getProperty(key)) + "\",");
//					
//					jsonBndInfo.append("\"" + BundleServiceSerializer.AVG_DATA_IN + "\": \"" +
//							BundleBandwidthCollection.getInstance().getSrvContainer(serviceRef).getAvgSrvIndata() + "\",");
//					jsonBndInfo.append("\"" + BundleServiceSerializer.AVG_DATA_OUT + "\": \"" +
//							BundleBandwidthCollection.getInstance().getSrvContainer(serviceRef).getAvgSrvOutdata() + "\",");
//					jsonBndInfo.append("\"" + BundleServiceSerializer.AVG_DATA_TOTAL + "\": \"" +
//							BundleBandwidthCollection.getInstance().getSrvContainer(serviceRef).getAvgSrvCommBandwidth() + "\",");
//
//					serviceBuffer.append(propertyBuffer.toString().substring(0, propertyBuffer.toString().length() - 1));
//					serviceBuffer.append("},");
//				}
//
//				jsonBndInfo.append(serviceBuffer.toString().substring(0, serviceBuffer.toString().length() - 1 ));
//				jsonBndInfo.append("]");
//				jsonBndInfo.append("},");
//		}
//
//		return jsonBndInfo.toString().substring(0, jsonBndInfo.toString().length() - 1) + "}}";
		return null;
	}

	public static String getXML(BundleContext context){
//		StringBuffer xmlBndInfo = new StringBuffer();
//
//		xmlBndInfo.append("<bnds>\n");
//		for (Bundle bundle : context.getBundles()){
//
//			if (bundle.getRegisteredServices() == null)
//				continue;
//
//			xmlBndInfo.append("<bnd> \n");
//				xmlBndInfo.append("<sn>" + Util.getBundleName(bundle) + "</sn>\n");
//				xmlBndInfo.append("<services> \n");
//
//				for (ServiceReference serviceRef : bundle.getRegisteredServices()){
//					
//					// doesn't consider proxy services as they are not the actual srevices
//					// and don't have to be taken into consideration
//					if (serviceRef.getProperty("proxied") != null)
//						continue;
//					
//					xmlBndInfo.append("<service> \n");
//
//					for (String key : serviceRef.getPropertyKeys())
//						xmlBndInfo.append("<" + key + ">" +  Util.getValueString(serviceRef.getProperty(key)) + "</" + key + ">\n");
//					
//					xmlBndInfo.append("<" + BundleServiceSerializer.AVG_DATA_IN + ">" +
//							BundleBandwidthCollection.getInstance().getSrvContainer(serviceRef).getAvgSrvIndata() + "</" + BundleServiceSerializer.AVG_DATA_IN + ">\n");
//					xmlBndInfo.append("<" + BundleServiceSerializer.AVG_DATA_OUT + ">" +
//							BundleBandwidthCollection.getInstance().getSrvContainer(serviceRef).getAvgSrvOutdata() + "</" + BundleServiceSerializer.AVG_DATA_OUT + ">\n");
//					xmlBndInfo.append("<" + BundleServiceSerializer.AVG_DATA_TOTAL + ">" +
//							BundleBandwidthCollection.getInstance().getSrvContainer(serviceRef).getAvgSrvCommBandwidth() + 
//							"</" + BundleServiceSerializer.AVG_DATA_TOTAL + ">\n");
//
//					xmlBndInfo.append("</service> \n");
//				}
//
//				xmlBndInfo.append("</services>\n");
//			xmlBndInfo.append("</bnd>");
//
//		}
//		xmlBndInfo.append("</bnds>");
//		return xmlBndInfo.toString();
		return null;
	}
	
	
//	The same function as the previous one with no Carriage Return, just for testing.
	public static String getXML_NOCR(BundleContext context){
//		StringBuffer xmlBndInfo = new StringBuffer();
//
//		xmlBndInfo.append("<bnds>");
//		for (Bundle bundle : context.getBundles()){
//
//			if (bundle.getRegisteredServices() == null)
//				continue;
//
//			xmlBndInfo.append("<bnd>");
//				xmlBndInfo.append("<sn>" + Util.getBundleName(bundle) + "</sn>");
//				xmlBndInfo.append("<services> ");
//
//				for (ServiceReference serviceRef : bundle.getRegisteredServices()){
//					
//					// doesn't consider proxy services as they are not the actual srevices
//					// and don't have to be taken into consideration
//					if (serviceRef.getProperty("proxied") != null)
//						continue;
//					
//					xmlBndInfo.append("<service>");
//
//					for (String key : serviceRef.getPropertyKeys())
//						xmlBndInfo.append("<" + key + ">" +  Util.getValueString(serviceRef.getProperty(key)) + "</" + key + ">");
//					
//					xmlBndInfo.append("<" + BundleServiceSerializer.AVG_DATA_IN + ">" +
//							BundleBandwidthCollection.getInstance().getSrvContainer(serviceRef).getAvgSrvIndata() + "</" + BundleServiceSerializer.AVG_DATA_IN + ">");
//					xmlBndInfo.append("<" + BundleServiceSerializer.AVG_DATA_OUT + ">" +
//							BundleBandwidthCollection.getInstance().getSrvContainer(serviceRef).getAvgSrvOutdata() + "</" + BundleServiceSerializer.AVG_DATA_OUT + ">");
//					xmlBndInfo.append("<" + BundleServiceSerializer.AVG_DATA_TOTAL + ">" +
//							BundleBandwidthCollection.getInstance().getSrvContainer(serviceRef).getAvgSrvCommBandwidth() + 
//							"</" + BundleServiceSerializer.AVG_DATA_TOTAL + ">");
//
//					xmlBndInfo.append("</service>");
//				}
//
//				xmlBndInfo.append("</services>");
//			xmlBndInfo.append("</bnd>");
//
//		}
//		xmlBndInfo.append("</bnds>");
//		return xmlBndInfo.toString();
		return null;
	}
}
